package cn.meye11.crm.controller;

import cn.meye11.crm.common.RestResponse;
import cn.meye11.crm.domain.model.RechargeTimes;
import cn.meye11.crm.domain.query.RechargeTimesQuery;
import cn.meye11.crm.service.RechargeTimesService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 充值记录表(RechargeTimes)表服务控制类
 * 
 * @author makejava
 * @since 2023-12-07 21:30:38
 */
@Slf4j
@Controller
@RequestMapping("recharge-times")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class RechargeTimesController {

    private final RechargeTimesService rechargeTimesService;

    // 代码写到此区域
    @GetMapping("manage")
    public String manage(Model model, HttpSession session) {
        model.addAttribute("user", session.getAttribute("user"));
        return "member/recharge-times";
    }
    
    /************************************* 请在上方编写新代码 **************************************/
    // 此区域为自动生成的通用代码 尽量不直接修改

    @PostMapping("save")
    @ResponseBody
    public RestResponse<Long> save(@RequestBody RechargeTimes model) {

        Long id = rechargeTimesService.save(model);
        return RestResponse.success("成功", id);
    }

    @DeleteMapping("remove/{id}")
    @ResponseBody
    public RestResponse<String> remove(@RequestBody RechargeTimes model) {

        rechargeTimesService.remove(model);
        return RestResponse.success("成功");
    }

    @PostMapping("update")
    @ResponseBody
    public RestResponse<String> update(@RequestBody RechargeTimes model) {

        if (model.getId() == null) {
            throw new RuntimeException("ID不能为空");
        }
        rechargeTimesService.update(model);
        return RestResponse.success("成功");
    }

    @GetMapping("get/{id}")
    @ResponseBody
    public RestResponse<RechargeTimes> get(Long id) {
        RechargeTimes model = rechargeTimesService.get(id);
        return model == null ? RestResponse.failed("不存在") : RestResponse.success("成功", model);
    }


    public RestResponse<Long> count(RechargeTimesQuery query) {
        Long count = rechargeTimesService.count(query);
        return RestResponse.success("成功", count);
    }

    @GetMapping("list")
    @ResponseBody
    public RestResponse<List<RechargeTimes>> list(RechargeTimesQuery query) {
        return rechargeTimesService.list(query);
    }

    @GetMapping("page")
    @ResponseBody
    public RestResponse<List<RechargeTimes>> page(RechargeTimesQuery query) {
        return rechargeTimesService.page(query);
    }
}
